// fenxiao/tixian/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tagindex: 2,//1小额提现2大额提现
    lists: [],
    isHolder: 0,//0表示非股东，1表示股东
    balance: 0,
    creatUid: 0,
    selMoney: 10
  },

  changeTagindex: function (e) {
    let tagindex = e.currentTarget.dataset.tagindex;
    this.setData({
      tagindex: tagindex
    })
  },
  changeMoney: function (e) {
    let selMoney = e.currentTarget.dataset.money;
    this.setData({
      selMoney: selMoney
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isHolder) {
      this.setData({
        isHolder: options.isHolder,
        balance: options.balance,
        creatUid: options.creatUid
      })
    }
  },
  //佣金提現(商家)
  formSubmit: function (e) {
    let that = this;
    var money = e.detail.value.money;
    var phone = e.detail.value.phone;
    if (app.globalData.userInfo.Uid == '') {
      return
    }
    let tagindex = that.data.tagindex;
    
    if (tagindex == '1') {//小额提现
      money = that.data.selMoney;
    }
    if (money == '') {
      wx.showModal({
        title: '提示',
        content: "请输入金额",
        showCancel: false
      })
      return
    }
    if (phone == '') {
      wx.showModal({
        title: '提示',
        content: "请输入电话",
        showCancel: false
      })
      return
    }
    else {
      wx.showModal({
        title: '提示',
        content: '是否确定提现！',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '申请中',
              mask: true,
            })

            wx.request({
              url: app.globalData.host + '/api/Distributor/BrokerageRequest',
              data: {
                userId: app.globalData.userInfo.Uid,
                storeId: app.globalData.storeId,
                money: money,
                phone: phone
              },
              success: function (res) {
                wx.hideLoading();
                if (res.data.Code == 0) {
                  wx.showToast({
                    title: '提现成功',
                    icon: 'success',
                    duration: 2000,
                    success: function () {
                      wx.navigateTo({
                        url: '/fenxiao/tixianRecords/index',
                      })
                    }
                  })
                } else {
                  wx.showModal({
                    title: '提示',
                    content: res.data.Message,
                    showCancel: false
                  })
                  return
                }
              }
            })
          } else if (res.cancel) {
            return
          }
        }
      })
    }
  },
  //获取分销商信息，如果分销商不存在，则创建新的
  GetDistributorInfo: function () {
    var that = this;
    var disId = wx.getStorageSync('disid')
    var agentId = wx.getStorageSync('agentid')
    if (disId == "" || disId == null) {
      disId = 0
    }
    if (agentId == "" || agentId == null) {
      agentId = 0
    }
    wx.request({
      url: app.globalData.host + '/api/Distributor/GetDistributorAndCreate',
      data: {
        userId: app.globalData.userInfo.Uid,
        disId: disId,
        agentId: agentId,
        storeId: app.globalData.storeId
      },
      success: function (res) {
        if (res.data.Code == 0) {
          var list = res.data.Data
          that.setData({
            lists: list,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 提现申请(股东)
  addRequest: function (e) {
    let that = this;
    var money = e.detail.value.money;
    var phone = e.detail.value.phone;
    if (app.globalData.userInfo.Uid == '') {
      return
    }
    if (money == '') {
      wx.showModal({
        title: '提示',
        content: "请输入金额",
        showCancel: false
      })
      return
    }
    if (phone == '') {
      wx.showModal({
        title: '提示',
        content: "请输入电话",
        showCancel: false
      })
      return
    }
    else {
      wx.showModal({
        title: '提示',
        content: '是否确定提现！',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '申请中',
              mask: true,
            })
            wx.request({
              url: app.globalData.host + '/api/Shareholder/AddRequest',
              data: {
                uid: app.globalData.userInfo.Uid,
                createUid: that.data.creatUid,
                storeId: app.globalData.storeId,
                amount: money,
                phone: phone
              },
              success: function (res) {
                wx.hideLoading();
                if (res.data.Code == 0) {
                  wx.showToast({
                    title: '申请成功',
                    icon: 'success',
                    duration: 2000,
                    success: function () {
                      wx.navigateTo({
                        url: '/fenxiao/tixianRecords/index?isHolder=1&creatUid=' + that.data.creatUid,
                      })
                    }
                  })
                } else {
                  wx.showModal({
                    title: '提示',
                    content: res.data.Message,
                    showCancel: false
                  })
                  return
                }
              }
            })
          } else if (res.cancel) {
            return
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.isHolder != 1) {
      this.GetDistributorInfo();
    }
  },
})